/**
 * Copyright &copy; 2016 CNJSON All rights reserved.
 */
package com.hongsuan.pplive.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cnjson.common.config.Global;
import com.cnjson.db.persistence.Page;
import com.cnjson.common.utils.StringUtils;
import com.cnjson.common.web.BaseController;
import com.hongsuan.pplive.modules.cms.entity.UserCourse;
import com.hongsuan.pplive.modules.cms.service.UserCourseService;

/**
 * 课程管理Controller
 * @author Susan
 * @version 2015-11-26
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/userCourse")
public class UserCourseController extends BaseController {

	@Autowired
	private UserCourseService userCourseService;
   

    @ModelAttribute
    public UserCourse get(UserCourse userCourse) {
        UserCourse entity = null;
        if (StringUtils.isNotBlank(userCourse.getId()) && StringUtils.isNoneBlank(userCourse.getCourseId())){
            entity = userCourseService.get(userCourse);
        }
        if (entity == null){
            entity = new UserCourse();
        }
        return entity;
    }

	@RequiresPermissions("cms:userCourse:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserCourse userCourse, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UserCourse> page = userCourseService.findPage(new Page<UserCourse>(request, response), userCourse);
		model.addAttribute("page", page);
		return "modules/cms/userCourseList";
	}

	@RequiresPermissions("cms:userCourse:view")
	@RequestMapping(value = "form")
	public String form(UserCourse userCourse, Model model) {
        UserCourse userCourse1 = get(userCourse);
		model.addAttribute("userCourse", userCourse1);
		return "modules/cms/userCourseForm";
	}

	@RequiresPermissions("cms:userCourse:edit")
	@RequestMapping(value = "save")
	public String save(UserCourse userCourse, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, userCourse)){
			return form(userCourse, model);
		}
        String[] courseIds = userCourse.getCourseId().split(",");
        for(String courseId : courseIds) {
            userCourse.setCourseId(courseId);
            userCourseService.save(userCourse);
        }

		addMessage(redirectAttributes, "给学员分配课程成功");
		return "redirect:"+Global.getAdminPath()+"/cms/userCourse/?repage";
	}

	@RequiresPermissions("cms:userCourse:edit")
	@RequestMapping(value = "delete")
	public String delete(UserCourse userCourse, RedirectAttributes redirectAttributes) {
		userCourseService.delete(userCourse);
		addMessage(redirectAttributes, "删除学员课程成功");
		return "redirect:"+Global.getAdminPath()+"/cms/userCourse/?repage";
	}

    @RequestMapping(value = "selectList")
    public String selectList(UserCourse userCourse, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<UserCourse> page = userCourseService.findUserPage(new Page<UserCourse>(request, response), userCourse);
        model.addAttribute("page", page);
        return "modules/cms/userSelectList";
    }
}